package com.spirng.leetcode.day06;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Test429 {
    public List<List<Integer>> levelOrder(Node root) {
        Queue<Node> queue=new LinkedList<>();
        ArrayList<List<Integer>> lists = new ArrayList<>();
        if(root==null){
            return lists;
        }
        queue.offer(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                Node node = queue.remove();
                for (Node child : node.children) {
                    queue.add(child);
                }
                list.add(node.val);
            }
            lists.add(list);
        }
        return lists;
    }
}

// Definition for a Node.
class   Node{
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
